<template>
    <div class="apply-list" v-loading="loading">
        <div class="input-box">
            <span class="input-title">用户ID</span>
            <el-input placeholder="用户ID" @keyup.enter.native="getList(1)" v-model.trim="paramsList.userId" style="width:120px;" clearable @clear="getList(1)">
            </el-input>
            <span class="input-title" style="margin-left: 20px;">昵称</span>
            <el-input placeholder="昵称" @keyup.enter.native="getList(1)" v-model.trim="paramsList.nickName" style="width:120px;" clearable @clear="getList(1)">
            </el-input>
            <span class="input-title" style="margin-left: 20px;">队长等级</span>
            <el-select @change="getList(1)" clearable style="width:120px;" v-model="paramsList.captainLevel">
                <el-option label="全部" :value="null"></el-option>
                <el-option label="见习队长" :value="1"></el-option>
                <el-option label="进阶队长" :value="2"></el-option>
                <el-option label="铜牌队长" :value="3"></el-option>
                <el-option label="银牌队长" :value="4"></el-option>
                <el-option label="金牌队长" :value="5"></el-option>
                <el-option label="皇冠队长" :value="6"></el-option>
            </el-select>
            <el-button type="primary" class="btn" style="margin-left:20px" @click="getList(1)">查询</el-button>
        </div>

        <div class="total-action">
            <el-button @click="totalChangeStatus(2)" type="text" size="small" style="color:#E6A23C;">批量进入考核</el-button>
            <el-button @click="totalChangeStatus(3)" type="text" size="small" style="color:red;">批量取消大神并缓冲</el-button>
        </div>

        <div class="table-box">
            <el-table :data="tableData" ref="table" @sort-change="sortChange" border style="width: 100%" @selection-change="selectionChange">
                <el-table-column
                    fixed="left"
                    type="selection"
                    :selectable="columnCanSelected"
                    width="40"></el-table-column>
                <el-table-column min-width="240" prop="gameName" label="用户">
                    <template slot-scope="scope">
                        <div class="table-flex">
                            <img class="user-avatar" :src="scope.row.icon" alt="">
                            <div class="user-info">
                                <p class="table-btn" @click="goUserDetail(scope.row.userId )">趣ID：{{ scope.row.userId }}</p>
                                <p>昵称：{{ scope.row.nickName }}</p>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column width="120" prop="createOrderCount" label="接单总场次" sortable="custom">
                    <template slot-scope="scope">
                        <div>{{ scope.row.createOrderCount || 0 }}</div>
                    </template>
                </el-table-column>
                <el-table-column width="100" prop="goodCommentPercent" label="好评率" sortable="custom">
                    <template slot-scope="scope">
                        <div v-if="scope.row.goodCommentPercent">{{ scope.row.goodCommentPercent }}%</div>
                        <div v-else>-</div>
                    </template>
                </el-table-column>
                <el-table-column width="100" prop="kingPercent" label="胜率" sortable="custom">
                    <template slot-scope="scope">
                        <div v-if="scope.row.kingPercent">{{ scope.row.kingPercent }}%</div>
                        <div v-else>-</div>
                    </template>
                </el-table-column>
                <el-table-column width="140" prop="complaintCount" label="被投诉次数" sortable="custom">
                    <template slot-scope="scope">
                        <div>{{ scope.row.complaintCount || 0 }}</div>
                    </template>
                </el-table-column>
                <el-table-column width="140" prop="maxHistoryJobLevel" label="最高段位" sortable="custom"></el-table-column>
                <el-table-column width="120" prop="jobLevel" label="可服务段位">
                    <template slot-scope="scope">
                        <span v-if="scope.row.jobLevel == 10">青铜</span>
                        <span v-if="scope.row.jobLevel == 11">白银</span>
                        <span v-if="scope.row.jobLevel == 12">黄金</span>
                        <span v-if="scope.row.jobLevel == 13">铂金</span>
                        <span v-if="scope.row.jobLevel === 14">钻石 </span>
                        <span v-if="scope.row.jobLevel === 15">星耀 </span>
                        <span v-if="scope.row.jobLevel === 16">王者0-25星 </span>
                        <span v-if="scope.row.jobLevel === 17">王者26-50星 </span>
                        <span v-if="scope.row.jobLevel === 18">王者51-75星 </span>
                        <span v-if="scope.row.jobLevel === 19">王者76-100星 </span>
                        <span v-if="scope.row.jobLevel === 20">王者101-125星 </span>
                        <span v-if="scope.row.jobLevel === 21">王者126-150星 </span>
                    </template>
                </el-table-column>
                <el-table-column width="120" prop="kingIncome" label="大神收益" sortable="custom">
                    <template slot-scope="scope">
                        <div>{{ scope.row.kingIncome || 0 }}</div>
                    </template>
                </el-table-column>
                <el-table-column width="120" prop="captainLevel" label="队长等级">
                    <template slot-scope="scope">
                        <span v-if="scope.row.captainLevel === 1">见习队长 </span>
                        <span v-if="scope.row.captainLevel === 2">进阶队长 </span>
                        <span v-if="scope.row.captainLevel === 3">铜牌队长 </span>
                        <span v-if="scope.row.captainLevel === 4">银牌队长 </span>
                        <span v-if="scope.row.captainLevel === 5">金牌队长 </span>
                        <span v-if="scope.row.captainLevel === 6">皇冠队长 </span>
                        <span v-if="!scope.row.captainLevel">-</span>
                    </template>
                </el-table-column>
                <el-table-column width="120" prop="commentTotal" label="评价数">
                    <template slot-scope="scope">
                        <div>{{ scope.row.commentTotal || 0 }}</div>
                    </template>
                </el-table-column>
                <el-table-column width="120" prop="okamiIntegral" label="大神积分">
                    <template slot-scope="scope">
                        <div>{{ scope.row.okamiIntegral || 0 }}</div>
                    </template>
                </el-table-column>
                <el-table-column width="120" prop="createOrderDay" label="接单天数">
                    <template slot-scope="scope">
                        <div>{{ scope.row.createOrderDay || 0 }}</div>
                    </template>
                </el-table-column>
                <el-table-column width="120" label="成为大神时间">
                    <template slot-scope="scope">
                        <span>{{ tableFormatDate(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column width="120" prop="okamiIntegral" label="大神状态">
                    <template slot-scope="scope">
                        <div v-if="scope.row.status === 1">正常</div>
                        <div v-if="scope.row.status === 2">考核中</div>
                        <div v-if="scope.row.status === 3">缓冲期</div>
                    </template>
                </el-table-column>
                <el-table-column fixed="right" width="130" label="操作">
                    <template slot-scope="scope">
                        <el-button style="margin-left: 10px;" @click="goLegendaryDetail(scope.row.userId)" type="text" size="small">查看详情</el-button>
                        <el-button style="margin-left: 10px;" @click="getApplyDetail(scope.row.userId)" type="text" size="small">查看申请资料</el-button>
                        <el-button @click="openJobLevelDialog(scope.row)" type="text" size="small" style="color:#67C23A;">设置可服务段位</el-button>
                        <el-button v-if="scope.row.status === 1" @click="changeStatus(scope.row, 2)" type="text" size="small" style="color:#E6A23C;">进入考核</el-button>
                        <el-button v-if="scope.row.status === 1" @click="changeStatus(scope.row, 3)" type="text" size="small" style="color:red;">取消大神并缓冲</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>

        <el-dialog class="dialog-box" title="设置可服务段位" :visible.sync="dialogVisible" width="260px">
            <div>
                <el-select clearable style="width:200px;" v-model="dialogParams.jobLevel">
                    <el-option label="铂金及以下" :value="13"></el-option>
                    <el-option label="星耀及以下" :value="15"></el-option>
                    <el-option label="王者25星及以下" :value="16"></el-option>
                    <el-option label="王者50星及以下" :value="17"></el-option>
                </el-select>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogCancel">取 消</el-button>
                <el-button type="primary" @click="setJobLevel">确 定</el-button>
            </span>
        </el-dialog>

        <el-dialog class="dialog-apply-box" title="大神申请详情" :visible.sync="applyDetailVisible" width="700px" center>
            <ApplyDetailDialog :tableData="applyTableData" />
        </el-dialog>
    </div>
</template>

<script>
import { formatDate } from '@/utils/utils'
import ApplyDetailDialog from './component/applyDetailDialog'

export default {
    components: {
        ApplyDetailDialog
    },
    name: 'legendaryList',
    props: {
        tabIndex: ''
    },
    data () {
        return {
            dialogParams: {
                jobLevel: ''
            },
            paramsList: {},
            tableData: [],
            applyTableData: [],
            skinList: [],
            pageNum: 1,
            pageSize: this._pageSize,
            total: 0,
            loading: false,
            dialogVisible: false,
            applyDetailVisible: false,
            userList: [] // 选中的用户列表
        }
    },
    watch: {
        tabIndex: {
            handler: function (val) {
                if (val === '1') {
                    this.getList(1)
                }
            },
            immediate: true
        }
    },
    methods: {
        // 获取列表
        async getList (pageNum) {
            if (this.loading) return
            this.loading = true
            this.pageNum = pageNum
            let params = {
                pageNum,
                pageSize: this.pageSize,
                sort: this.sort,
                type: this.orderColumn
            }
            params = Object.assign(params, this.paramsList)
            let res = await this.$http.get('/team/getKingList', { params })
            if (res.code === '0') {
                this.tableData = res.data
                this.total = res.count
            } else {
                this.$message({
                    type: 'error',
                    message: '系统异常，请稍后再试'
                })
            }
            this.loading = false
        },

        // 页码切换
        sizeChange (val) {
            this.pageNum = 1
            this.pageSize = val
            this.getList(1)
        },
        // 页容切换
        pageChange (val) {
            this.pageNum = val
            this.getList(this.pageNum)
        },

        setJobLevel () {
            this.$http.get('/team/setMaxJobLevel', {
                params: this.dialogParams
            }).then(res => {
                if (res.code === '0') {
                    this.currentItem.jobLevel = this.dialogParams.jobLevel
                    this.dialogParams.jobLevel = null
                    this.dialogVisible = false
                    this.$message({
                        type: 'success',
                        message: '修改成功'
                    })
                } else {
                    this.$message({
                        type: 'error',
                        message: '系统异常，请稍后再试'
                    })
                }
            })
        },

        // 当前行是否可以被选中
        columnCanSelected (row) {
            return row.status === 1
        },

        // 表格选中
        selectionChange (list) {
            this.userList = list
        },

        // 批量操作 2 进入考核期  3 取消大神资格进入缓冲期
        totalChangeStatus (status) {
            let len = this.userList.length
            if (!len) {
                this.$message({
                    type: 'warning',
                    message: '未选中用户，无效操作'
                })
                return
            }
            let msg = ''
            if (status === 2) {
                msg = `确定将 ${len}位 大神 列入考核期吗?`
            }
            if (status === 3) {
                msg = `确定取消 ${len}位用户的 的大神资格吗?`
            }
            this.$confirm(msg, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let userIdList = this.userList.map(item => {
                    return item.userId
                })
                this.setLegendaryStatus(userIdList, status, () => {
                    this.userList.forEach((item) => {
                        item.status = status
                    })
                    this.$refs.table.clearSelection()
                })
            }).catch(() => {
            })
        },

        // 改变用户大神状态 2 进入考核期  3 取消大神资格进入缓冲期
        changeStatus (item, status) {
            let msg = ''
            if (status === 2) {
                msg = `确定将大神 ${item.nickName}(id: ${item.userId}) 列入考核期吗?`
            }
            if (status === 3) {
                msg = `确定取消用户 ${item.nickName}(id: ${item.userId}) 的大神资格吗?`
            }
            this.$confirm(msg, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.setLegendaryStatus([item.userId], status, () => {
                    item.status = status
                    this.$refs.table.toggleRowSelection(item, false)
                })
            }).catch(() => {
            })
        },

        setLegendaryStatus (userIdList, status, cb) {
            this.$http.post('/team/setTeamKingStatus', {
                userIdList, status
            }).then(res => {
                if (res.code === '0') {
                    this.$message({
                        type: 'success',
                        message: '修改成功'
                    })
                    cb && cb()
                } else {
                    this.$message({
                        type: 'error',
                        message: '系统异常，请稍后再试'
                    })
                }
            })
        },
        openJobLevelDialog (item) {
            this.currentItem = item
            this.dialogParams.userId = item.userId
            this.dialogVisible = true
        },
        dialogCancel () {
            this.dialogParams.jobLevel = null
            this.dialogVisible = false
        },
        sortChange ({ column, prop, order }) {
            this.sort = order || ''
            // 如果没有 sort prop 置为空
            if (!this.sort) {
                this.type = null
            } else {
                if (this.sort === 'ascending') {
                    this.sort = 'asc'
                } else {
                    this.sort = 'desc'
                }
                this.orderColumn = prop
            }
            this.getList(1)
        },
        goLegendaryDetail (userId) {
            const { href } = this.$router.resolve({
                path: '/team/legendary/userDetail',
                query: {
                    userId
                }
            })
            window.open(href, '_blank')
        },
        getApplyDetail (userId) {
            let params = {
                userId
            }
            this.$http.get('/team/kingApplyView', { params }).then(res => {
                if (res.code === '0' && res.data) {
                    this.applyTableData = res.data.list || []
                    this.applyDetailVisible = true
                } else {
                    this.$message({
                        type: 'error',
                        message: '系统异常，请稍后再试'
                    })
                }
            })
        },
        goUserDetail (id) {
            const { href } = this.$router.resolve({
                path: '/dataReport/userInfo',
                query: {
                    id
                }
            })
            window.open(href, '_blank')
        },
        tableFormatDate (time) {
            return formatDate(time, 'YYYY-MM-DD')
        }
    }
}
</script>

<style lang="scss" scoped>
    .apply-list {
        .total-action {
            margin: 10px 0;
        }
        .table-box {
            .table-btn {
                cursor: pointer;
                color: #409EFF;
                font-size: 13px;
                font-weight: 500;
                &:hover {
                    text-decoration: underline;
                }
            }
        }
        .input-box {
            margin-top: 10px;
            font-size: 13px;
            font-weight: 500;
            .input-title {
                margin: 0 10px 0 0px;
            }
            .btn {
                margin-top: 10px;
                margin-left: 10px;
            }
        }
        .picture {
            width: 300px;
            height: 300px;
        }
        .table-flex {
            display: flex;
            align-items: center;
            .user-avatar {
                display: inline-block;
                min-width: 60px;
                height: 60px;
                margin-right: 5px;
                border-radius: 50%;
            }
            .user-info {
                display: flex;
                flex-direction: column;
                justify-content: center;
            }
        }

        .tools {
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }
    }
</style>
